<?php

namespace App\Http\Controllers;

use App\Helpers\ResponseHelper;
use App\Models\Config;
use App\Models\RunErrand;
use App\Models\School;
use App\Services\OrderService;
use Illuminate\Database\Eloquent\Builder;
use Illuminate\Http\JsonResponse;
use Illuminate\Http\Request;

class IndexController extends Controller
{
    /**
     * Display a listing of the resource.
     *
     * @return \Illuminate\Http\Response
     */
    public function index(Request $request): JsonResponse
    {
        $schoolId = $request->input('school_id') ?? 1;
        $school = School::with(['faculty', 'unitBlock', 'banner', 'bulletin', 'commission'])->where('status', 2)->findOrFail($schoolId);
        return ResponseHelper::success(data: $school);
    }

    public function schoolList(Request $request): JsonResponse
    {
        $name = $request->input('name') ?? '';
        $list = School::with(['faculty', 'unitBlock'])
            ->when($name, function (Builder $builder
            ) use ($name) {
                $builder->where('name', 'like', '%' . $name . '%');
            })->where('status', 2)->get();
        return ResponseHelper::success(data: $list);
    }

    public function customer()
    {
        $customer = Config::where('id', 1)->first()->value;
        return ResponseHelper::success(data: $customer);
    }

    public function disclaimer()
    {
        $disclaimer = Config::where('id', 2)->first()->value;
        return ResponseHelper::success(data: $disclaimer);
    }

    public function withdrawalInstructions()
    {
        $desc = Config::where('id', 4)->first()->value;
        return ResponseHelper::success(data: $desc);
    }

    public function removeBlacklist()
    {
        RunErrand::where('blacklists', 2)->get()->map(function (RunErrand $runErrand) {
            $runErrand->blacklists = 1;
            $runErrand->save();
        });
        return ResponseHelper::success(message: '操作成功');
    }
}
